package com.selfdriving.activity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.selfdriving.BaseApplication;
import com.selfdriving.bean.Preference;
import com.selfdriving.utils.LogUtil;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.provider.SyncStateContract.Constants;
import android.support.v4.view.ViewPager.LayoutParams;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.EventListener;
import net.tsz.afinal.annotation.view.Select;
import net.tsz.afinal.annotation.view.ViewInject;

public class BaseActivity extends FinalActivity {
	
	public static String LOGTAG = BaseActivity.class.getSimpleName();
	
	public static SharedPreferences mShared;
	
	public SharedPreferences sharedPrefs;
	
	public static SharedPreferences.Editor mEditor;
	
	protected LinearLayout.LayoutParams layoutParams;
	
	protected BaseApplication mApplication;
	
	protected List<AsyncTask<Void, Void, Boolean>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, Boolean>>();

	public BaseActivity() {
		super();
		LOGTAG = this.getClass().getSimpleName();
	}

	protected Handler mHanler;

	/**
	 * ���_�@ʾ��dialog
	 */
	private ProgressDialog mProgressDialog;

	private final ArrayList<LifeCycleListener> mListeners = new ArrayList<LifeCycleListener>();

	public static interface LifeCycleListener {
		public void onActivityCreated(BaseActivity activity);

		public void onActivityDestroyed(BaseActivity activity);

		public void onActivityPaused(BaseActivity activity);

		public void onActivityResumed(BaseActivity activity);

		public void onActivityStarted(BaseActivity activity);

		public void onActivityStopped(BaseActivity activity);
	}

	public static class LifeCycleAdapter implements LifeCycleListener {
		public void onActivityCreated(BaseActivity activity) {
		}

		public void onActivityDestroyed(BaseActivity activity) {
		}

		public void onActivityPaused(BaseActivity activity) {
		}

		public void onActivityResumed(BaseActivity activity) {
		}

		public void onActivityStarted(BaseActivity activity) {
		}

		public void onActivityStopped(BaseActivity activity) {
		}
	}

	public void addLifeCycleListener(LifeCycleListener listener) {
		if (mListeners.contains(listener))
			return;
		mListeners.add(listener);
	}

	public void removeLifeCycleListener(LifeCycleListener listener) {
		mListeners.remove(listener);
	}
	
	public void putAsyncTask(AsyncTask<Void, Void, Boolean> asyncTask)
	{
		mAsyncTasks.add(asyncTask.execute());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mHanler = new Handler();
		for (LifeCycleListener listener : mListeners) {
			listener.onActivityCreated(this);
		}
		LogUtil.d("[BaseActivity onCreate] : " + LOGTAG);
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
			LogUtil.d("[BaseActivity onCreate] : " + LOGTAG
					+ ", setRequestedOrientation");
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		mShared = this.getSharedPreferences(Preference.PREFERENCES_NAME, 0);
		mEditor = mShared.edit();
		layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mApplication = (BaseApplication) getApplication();
	}

	public static boolean putString(final String entry, String value,
			boolean commit) {
		if (mEditor == null) {
			return false;
		}
		mEditor.putString(entry, value);
		if (commit) {
			return mEditor.commit();
		}
		return true;
	}

	public static boolean putInt(final String entry, int value, boolean commit) {
		if (mEditor == null) {
			return false;
		}
		mEditor.putInt(entry, value);
		if (commit) {
			return mEditor.commit();
		}
		return true;
	}

	public static boolean putBoolean(final String entry, boolean value,
			boolean commit) {
		if (mEditor == null) {
			return false;
		}
		mEditor.putBoolean(entry, value);
		if (commit) {
			return mEditor.commit();
		}
		return true;
	}

	public boolean getBoolean(final String entry, boolean defaultValue) {
		if (mEditor == null) {
			return defaultValue;
		}
		try {
			return mShared.getBoolean(entry, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static String getString(final String entry, String defaultValue) {
		if (mEditor == null) {
			return defaultValue;
		}
		try {
			return mShared.getString(entry, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public int getInt(final String entry, int defaultValue) {
		if (mEditor == null) {
			return defaultValue;
		}
		try {
			return mShared.getInt(entry, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	@Override
	protected void onDestroy() {
		LogUtil.d("[BaseActivity onDestroy] : " + LOGTAG);
		super.onDestroy();
		cancelInProgressOnUiThread();
		for (LifeCycleListener listener : mListeners) {
			listener.onActivityDestroyed(this);
		}
	}

	@Override
	protected void onStart() {
		LogUtil.d("[BaseActivity onStart] : " + LOGTAG);
		super.onStart();
		for (LifeCycleListener listener : mListeners) {
			listener.onActivityStarted(this);
		}
	}

	@Override
	protected void onPause() {
		LogUtil.d("[BaseActivity onPause] : " + LOGTAG);
		super.onPause();
	}

	@Override
	protected void onResume() {
		LogUtil.d("[BaseActivity onResume] : " + LOGTAG);
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
		LogUtil.d("[BaseActivity onStop] : " + LOGTAG);
		for (LifeCycleListener listener : mListeners) {
			listener.onActivityStopped(this);
		}
	}

	private Toast toast;

	/**
	 * 显示toast 短时
	 * 
	 * @param message
	 */
	public void showToast(String message) {
		if (toast == null) {
			toast = Toast.makeText(BaseActivity.this, message,
					Toast.LENGTH_SHORT);
		} else {
			if (toast.getView() != null
					&& toast.getView().getWindowToken() != null) {
				toast.cancel();
			}
			toast.setText(message);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
	
	/** 含有标题、内容、两个按钮的对话框 **/
	protected AlertDialog showAlertDialog(String title, String message,
			String positiveText,
			DialogInterface.OnClickListener onPositiveClickListener,
			String negativeText,
			DialogInterface.OnClickListener onNegativeClickListener)
	{
		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(message)
				.setPositiveButton(positiveText, onPositiveClickListener)
				.setNegativeButton(negativeText, onNegativeClickListener)
				.show();
		return alertDialog;
	}

	/**
	 * 封装 handler消息
	 */
	public void getHanderMessage(Handler handler, int what, String details) {
		handler.sendMessage(Message.obtain(handler, what, details));
	}

	public SharedPreferences getSharedPreferences() {
		return sharedPrefs;
	}
	
	/**
	 * 线程取消进度条
	 */
	public void cancelInProgressOnUiThread() {
		mHanler.post(new Runnable() {
			@Override
			public void run() {
				cancelInProgress();
			}
		});
	}
	
	/**
	 * 取消进度条
	 */
	public void cancelInProgress() {
		synchronized (this) {
			if (mProgressDialog != null) {
				mProgressDialog.cancel();
				mProgressDialog = null;
			}
		}
	}
	
}